Imports System.Windows.Forms
Imports System.Xml
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Text.RegularExpressions

Friend Class frmCarSelection
    Private m_car As CarBase = Nothing
    Private m_configs As ConfigHolder = Nothing

    Friend Sub New(ByRef Car As CarBase)

        InitializeComponent()

        m_car = Car
        Dim Deserializer As New BinaryFormatter
        '' Open file and load its contents
        Try
            Dim f_encStream As MemoryStream

            If File.Exists(Application.StartupPath & "\Config-Override.enc") Then
                f_encStream = New MemoryStream(arrayLoad(Application.StartupPath & "\Config-Override.enc"))
            Else
                f_encStream = (getResource("Config.enc"))
            End If

            Dim f_memstream As New MemoryStream()

            CryptFile("Get Them Config's out!", f_encStream, f_memstream, False)
            Dim f_newStream As New MemoryStream(f_memstream.ToArray)
            m_configs = CType(Deserializer.Deserialize(f_newStream), ConfigHolder)

            For Each config As CarConfig In m_configs.getAsArray
                Me.cmbSelect.Items.Add(New String(config.Series & " - " & config.Engine))
            Next
        Catch ex As Exception
            Err_message("Error Loading Configurations")
            Err_report(Err.Description)
        End Try
    End Sub
    Friend Function getConfig() As Boolean
        If Not IsNothing(m_configs) Then
            For Each config As CarConfig In m_configs.getAsArray
                If m_car.ConfigID = config.ConfigID Then
                    m_car.setConfig(config)
                    CheckConfig()
                    Return True
                    Exit Function
                End If
            Next
        End If
        Return False
    End Function
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Try
            m_car.setConfig(m_configs.item(cmbSelect.SelectedIndex))
            m_car.ConfigID = m_car.getConfig.ConfigID
            CheckConfig()
            Console.WriteLine("Globals: " & m_car.getConfig.getGlobals.count)
            Console.WriteLine("BikiRom: " & m_car.getConfig.getBikiVars.count)
            Console.WriteLine("Tables: " & m_car.getConfig.getTables.count)

            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        Catch
            Err_message("Error Selecting Configuration")
        End Try
    End Sub
    Private Sub CheckConfig()
        For Each row As BVar In m_car.getConfig.getBikiVars.getAsArray
            If IsNothing(m_car.getVars.getByName(row.Name)) Then
                Dim f_add As BVar = ObjectCopy(row)
                f_add.setNext(Nothing)
                m_car.getVars.addNode(f_add)
            End If
        Next
        If Not IsNothing(m_car.getConfig.getBikiVars.getByName("flashstart")) Then
            m_car.getVars.getByName("flashstart").Value = m_car.getConfig.getBikiVars.getByName("flashstart").Value
        End If
        If Not IsNothing(m_car.getConfig.getBikiVars.getByName("flashend")) Then
            m_car.getVars.getByName("flashend").Value = m_car.getConfig.getBikiVars.getByName("flashend").Value
        End If
        If Not IsNothing(m_car.getConfig.getBikiVars.getByName("27c256")) Then
            m_car.getVars.getByName("27c256").Value = m_car.getConfig.getBikiVars.getByName("27c256").Value
        End If
        m_car.DataStart = m_car.getVars.getByName("flashstart").Value
        m_car.DataEnd = m_car.getVars.getByName("flashend").Value
    End Sub
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

End Class
